package com.jack.helloworld.activity;

import android.graphics.Color;
import android.os.Bundle;
import androidx.viewpager.widget.PagerTabStrip;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.jack.helloworld.R;
import com.jack.helloworld.adapter.ViewPageAdapter;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    ViewPager viewPager;
    ViewPageAdapter viewPageAdapter;
    PagerTabStrip pagerTabStrip;

    List<View> listView;
    List<String> listPageViewTitle;

    View view1;
    View view2;
    View view3;
    View view4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);

        initView();
        initDate();
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        pagerTabStrip = (PagerTabStrip) findViewById(R.id.pagerTabStrip);
        view1 = View.inflate(this, R.layout.view1, null);
        view2 = View.inflate(this, R.layout.view2, null);
        view3 = View.inflate(this, R.layout.view3, null);
        view4 = View.inflate(this, R.layout.view4, null);

        pagerTabStrip.setTabIndicatorColor(Color.GRAY);
        pagerTabStrip.setDrawFullUnderline(false);
    }

    private void initDate() {
        listView = new ArrayList<View>();
        listPageViewTitle = new ArrayList<String>();

        listView.add(view1);
        listView.add(view2);
        listView.add(view3);
        listView.add(view4);

        listPageViewTitle.add("title1");
        listPageViewTitle.add("title2");
        listPageViewTitle.add("title3");
        listPageViewTitle.add("title4");

        viewPageAdapter = new ViewPageAdapter(listView, listPageViewTitle);
        viewPager.setAdapter(viewPageAdapter);

        viewPager.addOnPageChangeListener(this);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Toast.makeText(this, "position:" + (position + 1), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
